package com.cms.web.exportQueryAnalyse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.metadata.biz.MetaDataServiceMgr;
import com.persistence.qxgl.beans.SYSUser;
import com.pub.util.EMPExportExcelTable;
import com.web.BaseController;

import com.cms.biz.business.CMSGeneralContractServiceMgr;
import com.cms.biz.queryAnalyse.CMSQueryAnalyseServiceMgr;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.eai.data.JResponseObject;
import com.enums.EMPQueryParamEnum;

@Controller
@RequestMapping(value="/cmsContractQueryAnalyse/export")
public class EMPExportCMContractQueryAnalyseController extends BaseController{
	@Autowired
	private                       MetaDataServiceMgr                              MetaDataService;

	@Autowired
	public HttpSession session;
	
	@Autowired
	private                CMSQueryAnalyseServiceMgr                       queryAnalyseServiceMgr;
	
	@Autowired
	private             CMSGeneralContractServiceMgr                           generalContractMgr;
	
	/**
	 * 销售合同
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/contractList")
	public ModelAndView XSContractList(@RequestParam String F_DJLX, @RequestParam String F_HTMC, 
            						   HttpServletRequest request, HttpServletResponse response, Model model) {
		EFDataSet        columnDS = EFDataSet.getInstance();
		EFRowSet         columnRS = null;
		EFDataSet    queryDataSet = null;
		List<String>  subHeadList = new ArrayList<String>();
		JParamObject           PO = JParamObject.Create();
		SYSUser              user = (SYSUser) session.getAttribute("contextUser");
		String         BILL_WHERE = " F_DJLX like '" + F_DJLX + "%' and F_HTMC like '" + F_HTMC + "%' ";
		String           fileName = "";
		EFDataSet          metaDS = null;
		
		if(F_DJLX.equals("WXHT")) {
			BILL_WHERE += " and F_BMBH = '" + user.getUSER_ORGID() + "'";
			fileName = "维 修 合 同 信 息";
		} else {
			fileName = "销 售 合 同 信 息";
		}
		PO.SetValueByParamName("TABLE_NAME", "HTXXZD");
		PO.SetValueByParamName("F_DJLX", F_DJLX);
		PO.SetValueByParamName("sqlWhere", BILL_WHERE);
		PO.SetValueByParamName("sqlOrderBy", " HTXXZD.F_HTBH desc");
		queryDataSet = generalContractMgr.loadCMSGeneralContract(PO);

		try {
			metaDS = MetaDataService.getSYS_OBJCOLS("HTXXZD");
			for(int i = 0; i < metaDS.getRowCount(); i++) {
				if(metaDS.getRowSet(i).getString("COL_ID", "").equals("F_GUID") || metaDS.getRowSet(i).getString("COL_ID", "").equals("F_HTJE")) {
					continue;
				}
				columnRS = EFRowSet.getInstance();
				columnRS.putString("COLUMN_ID", metaDS.getRowSet(i).getString("COL_ID", ""));
				columnRS.putString("COLUMN_NAME", metaDS.getRowSet(i).getString("COL_MC", ""));
				columnRS.putString("COLUMN_WIDTH", "100");
				columnRS.putString("COLUMN_TYPE", metaDS.getRowSet(i).getString("COL_TYPE", ""));
				if(metaDS.getRowSet(i).getString("COL_TYPE", "").equals("N")) {
					columnRS.putString("COLUMN_ALIGN", "RIGHT");
				} else {
					columnRS.putString("COLUMN_ALIGN", "LEFT");
				}
				if(metaDS.getRowSet(i).getString("COL_ID", "").equals("F_SYZT")) {
					Map<String, String> maskMap = new HashMap<String, String>();
					maskMap.put("0", "停用");
					maskMap.put("1", "启用");
					columnRS.setExtProperty("MASKMAP", maskMap);
				} else if(metaDS.getRowSet(i).getString("COL_ID", "").equals("F_SHZT")) {
					Map<String, String> maskMap = new HashMap<String, String>();
					maskMap.put("0", "全部");
					maskMap.put("1", "部分");
					maskMap.put("2", "未送");
					columnRS.setExtProperty("MASKMAP", maskMap);
				} else if(metaDS.getRowSet(i).getString("COL_ID", "").equals("F_DJLX")) {
					Map<String, String> maskMap = new HashMap<String, String>();
					maskMap.put("XSHT", "销售合同");
					maskMap.put("WXHT", "维修合同");
					columnRS.setExtProperty("MASKMAP", maskMap);
				}
				columnDS.insertRowSet(columnRS);
			}
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_HTJE");
			columnRS.putString("COLUMN_ALIGN", "RIGHT");
			columnRS.putString("COLUMN_NAME", "合同金额");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "N");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_YFK");
			columnRS.putString("COLUMN_ALIGN", "RIGHT");
			columnRS.putString("COLUMN_NAME", "已付金额");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "N");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_WFK");
			columnRS.putString("COLUMN_ALIGN", "RIGHT");
			columnRS.putString("COLUMN_NAME", "未付金额");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "N");
			columnDS.insertRowSet(columnRS);
			//下载
			EMPExportExcelTable exportExcelTable = new EMPExportExcelTable();
			exportExcelTable.exportExcelTable(request, response, queryDataSet, columnDS, subHeadList, fileName);			
		} catch (Exception e) {

			e.printStackTrace();
		}
		return ajaxDoneError("导出成功！");
	}
	
	
	@RequestMapping("/unpaidContractInfoQuery")
	public ModelAndView unpaidContractInfoQuery(@RequestParam String F_DJLX, @RequestParam String F_HTMC, HttpServletRequest request, HttpServletResponse response, Model model) {
		EFDataSet        columnDS = EFDataSet.getInstance();
		EFRowSet         columnRS = null;
		EFDataSet    queryDataSet = null;
		List<String>  subHeadList = new ArrayList<String>();
		JParamObject           PO = JParamObject.Create();
		EFDataSet          metaDS = null;
		EFRowSet      queryRowSet = null;
		String           fileName = "";
		
		PO.SetValueByParamName("F_HTMC", F_HTMC);
		PO.SetValueByParamName("F_DJLX", F_DJLX);
		JResponseObject        RO = queryAnalyseServiceMgr.searchUnpaidContractInfo(PO);
		queryRowSet = (EFRowSet) RO.getResponseObject();
		
		queryDataSet = queryRowSet.getDataSet(EMPQueryParamEnum.QUERYRESULT);
		
		try {
			if(F_DJLX.equals("WXHT")) {
				fileName = "维 修 合 同 未 付 款 信 息";
			} else {
				fileName = "销 售 合 同 未 付 款 信 息";
			}
			metaDS = MetaDataService.getSYS_OBJCOLS("HTXXZD");
			for(int i = 0; i < metaDS.getRowCount(); i++) {
				if(metaDS.getRowSet(i).getString("COL_ID", "").equals("F_GUID") || metaDS.getRowSet(i).getString("COL_ID", "").equals("F_HTJE")) {
					continue;
				}
				columnRS = EFRowSet.getInstance();
				columnRS.putString("COLUMN_ID", metaDS.getRowSet(i).getString("COL_ID", ""));
				columnRS.putString("COLUMN_NAME", metaDS.getRowSet(i).getString("COL_MC", ""));
				columnRS.putString("COLUMN_WIDTH", "100");
				columnRS.putString("COLUMN_TYPE", metaDS.getRowSet(i).getString("COL_TYPE", ""));
				if(metaDS.getRowSet(i).getString("COL_TYPE", "").equals("N")) {
					columnRS.putString("COLUMN_ALIGN", "RIGHT");
				} else {
					columnRS.putString("COLUMN_ALIGN", "LEFT");
				}
				if(metaDS.getRowSet(i).getString("COL_ID", "").equals("F_SYZT")) {
					Map<String, String> maskMap = new HashMap<String, String>();
					maskMap.put("0", "停用");
					maskMap.put("1", "启用");
					columnRS.setExtProperty("MASKMAP", maskMap);
				} else if(metaDS.getRowSet(i).getString("COL_ID", "").equals("F_SHZT")) {
					Map<String, String> maskMap = new HashMap<String, String>();
					maskMap.put("0", "全部");
					maskMap.put("1", "部分");
					maskMap.put("2", "未送");
					columnRS.setExtProperty("MASKMAP", maskMap);
				} else if(metaDS.getRowSet(i).getString("COL_ID", "").equals("F_DJLX")) {
					Map<String, String> maskMap = new HashMap<String, String>();
					maskMap.put("XSHT", "销售合同");
					maskMap.put("WXHT", "维修合同");
					columnRS.setExtProperty("MASKMAP", maskMap);
				}
				columnDS.insertRowSet(columnRS);
			}
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_HTJE");
			columnRS.putString("COLUMN_ALIGN", "RIGHT");
			columnRS.putString("COLUMN_NAME", "合同金额");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "N");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_YFK");
			columnRS.putString("COLUMN_ALIGN", "RIGHT");
			columnRS.putString("COLUMN_NAME", "已付金额");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "N");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_WFK");
			columnRS.putString("COLUMN_ALIGN", "RIGHT");
			columnRS.putString("COLUMN_NAME", "未付金额");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "N");
			columnDS.insertRowSet(columnRS);
			//下载
			EMPExportExcelTable exportExcelTable = new EMPExportExcelTable();
			exportExcelTable.exportExcelTable(request, response, queryDataSet, columnDS, subHeadList, fileName);			
		} catch (Exception e) {

			e.printStackTrace();
		}
		return ajaxDoneError("导出成功！");
	}
	
	/**
	 * 已付款合同查询
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/paidContractInfoQuery/{F_GUID}")
	public ModelAndView paidContractInfoQuery(@PathVariable("F_GUID") String F_GUID, HttpServletRequest request, HttpServletResponse response, Model model) {
		JParamObject           po = JParamObject.Create();
		EFDataSet    queryDataSet = null;
		EFRowSet      queryRowSet = null;
		EFDataSet        columnDS = EFDataSet.getInstance();
		EFRowSet         columnRS = null;
		List<String>  subHeadList = new ArrayList<String>();
		po.SetValueByParamName("F_GUID", F_GUID);
		
		JResponseObject        RO = queryAnalyseServiceMgr.searchPaidContractInfo(po);
		queryRowSet = (EFRowSet) RO.getResponseObject();
		
		queryDataSet = queryRowSet.getDataSet(EMPQueryParamEnum.QUERYRESULT);
		try {
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_KJQJ");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "会计年度");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_DJBH");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "单据编号");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_HTBH");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "合同编号");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_HTBHMC");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "合同名称");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_FKCS");
			columnRS.putString("COLUMN_ALIGN", "RIGHT");
			columnRS.putString("COLUMN_NAME", "付款次数");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "N");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_FKJE");
			columnRS.putString("COLUMN_ALIGN", "RIGHT");
			columnRS.putString("COLUMN_NAME", "付款金额");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "N");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_BZ");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "备注");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			//下载
			EMPExportExcelTable exportExcelTable = new EMPExportExcelTable();
			exportExcelTable.exportExcelTable(request, response, queryDataSet, columnDS, subHeadList, "合 同 付 款 信 息");			
		} catch (Exception e) {

			e.printStackTrace();
		}
		return ajaxDoneError("导出成功！");
	}
}
